//
//  AVGMainTabBarController.swift
//  33-
//
//  Created by mac on 16/7/19.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class AVGMainTabBarController: UITabBarController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
    // 设置自定义tabBar
    let mainTabBar = AVGMainTabBar()
        
    // 定义和传递闭包
    mainTabBar.composeCallback =
        
    { () -> Void in
        print("我在控制器里面知道加号按钮点击了")
            
            // TODO:
    }
        
    // 设置tabbarVC的tabBar
    // = 直接赋值, get-only 就不能直接赋值
    //        self.tabBar = mainTabBar
    // 间接赋值, KVC
    self.setValue(mainTabBar, forKey: "tabBar")
    // Do any additional setup after loading the view.
    addChildViewController()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
    //添加所有的自控制器
    func  addChildViewController() {
    // 添加子控制器
    // 首页
    let homeVC = AVGhomeTableViewController()
    addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
    // 消息
    let messageVC = AVGMessageTableViewController()
    addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
    // 发现
    let discoveryVC = AVGFindTableViewController()
    addChildViewController(discoveryVC, title: "发现", imageName: "tabbar_discover")
        
    // 我
    let profileVC = AVGMeTableViewController()
    addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")

    }
   
    func addChildViewController(controller: UIViewController, title: String, imageName: String) {
    // 设置标题和图片
    controller.title = title
        
    // 设置选中时字体颜色
    controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
    controller.tabBarItem.image = UIImage(named: imageName)
        
    // 拼接选中图片的名称
    let selectedImageName = imageName + "_selected"
    // 设置选中的图片
    controller.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
    self.addChildViewController(UINavigationController(rootViewController: controller))
    }
    
}
